package com.zr.util

import com.alibaba.fastjson.JSON
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import java.lang.Exception
import javax.servlet.http.HttpServletResponse
//这个标签必不可少
@ControllerAdvice
class GlobalExceptionHandler {
//表示返回的值不是视图
    @ResponseBody
    //标记为异常处理函数并设置所处理的异常的类型
    @ExceptionHandler(value = arrayOf(Exception::class))
    fun exceptionHandler(response:HttpServletResponse,e:Exception):String{
        //打印出错信息
        e.printStackTrace()
        //设置浏览器出错代码
        response.status=HttpServletResponse.SC_INTERNAL_SERVER_ERROR
        //向前端返回格式化出错信息
        return JSON.toJSONString(Result(false,"CRITICAL ERROR!",e.message))
    }
}